package com.example.demo.zaqizabatest.leetcode.day7;

/**
 * 输入两棵二叉树A和B，判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
 *
 * B是A的子结构， 即 A中有出现和B相同的结构和节点值。
 */
public class Solution2 {

    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode mirrorRoot = new TreeNode(root.val);
        mirrorNodes(root, mirrorRoot);
        return mirrorRoot;
    }

    private void mirrorNodes(TreeNode node, TreeNode mirrorNode) {
        if (node.left != null) {
            mirrorNode.right = new TreeNode(node.left.val);
            mirrorNodes(node.left, mirrorNode.right);
        }
        if (node.right != null) {
            mirrorNode.left = new TreeNode(node.right.val);
            mirrorNodes(node.right, mirrorNode.left);
        }
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
